package jalus.pojo;

import java.util.ArrayList;
import java.util.List;

/**
 * 分页
 * 
 *
 */
public class Pageindicator {
	/** 当前页数 页数从1开始 **/
	public int currentPageNum;
	/** 总页数 **/
	public int totalPageNum;
	/** 每页文章数 **/
	public int itemNumPerPage;
	/** 总文章数 **/
	public int totalItemNum;
	/** 当前页起始文章位置，从0开始 **/
	public int startpos;
	/** 当前页文章数 **/
	public int postNum;
	/** 当前页前后显示的页数 **/
	public int showpagenum=5;
	private int beginpage;
	private int endpage;
	public Pageindicator(int totalPostNum, int currentPageNum, int postNumPerPage) {
		this.totalItemNum = totalPostNum;
		this.currentPageNum = currentPageNum;
		this.itemNumPerPage = postNumPerPage;
		
		this.totalPageNum = (this.totalItemNum-1)/this.itemNumPerPage + 1;
		if(this.currentPageNum > this.totalPageNum){
			this.currentPageNum = this.totalPageNum;
		}
		this.startpos = (this.currentPageNum-1) * this.itemNumPerPage;
		if(this.currentPageNum != this.totalPageNum){
			this.postNum = this.itemNumPerPage;
		}else{
			this.postNum = this.totalItemNum % this.itemNumPerPage;
		}
		
		
		this.beginpage = 1;
		if(this.beginpage < this.currentPageNum - 5){
			this.beginpage = this.currentPageNum - 5;
		}
		this.endpage = this.totalPageNum;
		if(this.endpage > this.currentPageNum + 5){
			this.endpage = this.currentPageNum + 5;
		}
		int diff = this.endpage - this.beginpage;
		if(diff < 10){
			if(this.beginpage == 1){
				this.endpage += 10 - diff;
				if(this.endpage > this.totalPageNum){
					this.endpage = this.totalPageNum;
				}
			}
			
			if(this.endpage == this.totalPageNum){
				this.beginpage -= 10 - diff;
				if(this.beginpage < 1){
					this.beginpage = 1;
				}
			}
			
		}
	}
	public boolean hasNext(){
		return this.currentPageNum < this.totalPageNum;
	}
	public boolean hasPrev(){
		return this.currentPageNum > 1;
	}
	public int getFirstPage(){
		return 1;
	}
	public int getLastPage(){
		return this.totalPageNum;
	}
	public int getPrevPage(){
		return this.currentPageNum - 1;
	}
	public int getNextPage(){
		return this.currentPageNum + 1;
	}
	public int getCurrentPageNum(){
		return this.currentPageNum;
	}
	public int getTotalPageNum(){
		return this.totalPageNum;
	}
	public int getCurrentStartIndex(){
		return this.startpos;
	}
	public int getCurrentEndIndex(){
		return this.startpos + this.itemNumPerPage;
	}
	public int getTotalItemNum(){
		return this.totalItemNum;
	}
	public List<Integer> getAllPages(){

		List<Integer> pageList = new ArrayList<Integer>(this.totalPageNum);
		for(int i=this.beginpage; i<=this.endpage; i++){
			pageList.add(i);
		}
		return pageList;
	}
	
	public boolean isBeginDot(){
		return this.beginpage > 1;
	}
	
	public boolean isEndDot(){
		return this.endpage < this.totalPageNum;
	}
}
